package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Moments;
import com.dnjn.happiness.biz.service.IMomentsService;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.HxBeanSeed;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import com.dnjn.happiness.emo.api.RemoteHlbeanTypeService;
import com.dnjn.happiness.emo.api.model.HlbeanTypeVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 圈子 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-10-04
 */
@Tag(name = "biz Moments 圈子", description = "圈子")
@RestController
@RequestMapping("/moments")
public class MomentsController extends BaseController {

    @Autowired
    protected IMomentsService baseService;

    @Autowired
    private RemoteEmoUserService hxBeanService;

    @Autowired
    private RemoteHlbeanTypeService hlbeanTypeService;

    @RequiresPermissions("biz:moments:query")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        Moments domain = baseService.getOneById(id);
        domain.setImages(baseService.selectImageUrlByMomentsId(domain.getMomentsId()));
        return success(domain);
    }

    @RequiresPermissions("biz:moments:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Moments domain) {
        domain.setCreateBy(SecurityUtils.getUsername());
        domain.setUserId(SecurityUtils.getUserId());
        domain.setDateCurrent(LocalDateTime.now());
        baseService.createDomain(domain);
        //新增快乐豆
        Integer count = baseService.selectCount(domain.getUserId());
        HlbeanTypeVO hlbeanType = new HlbeanTypeVO();
        R<HlbeanTypeVO> resultVO = hlbeanTypeService.getHlbeanTypeByCode(Constants.MOMENTS_HLBEAN_CODE, SecurityConstants.INNER);
        if (StringUtils.isNotNull(resultVO) && StringUtils.isNotNull(resultVO.getData())) {
            hlbeanType = resultVO.getData();
        }
        Integer maxCount = hlbeanType.getMaxCountPreDay();
        if (count <= maxCount) {
            HxBeanSeed beanSeed = new HxBeanSeed();
            beanSeed.setUserId(domain.getUserId());
            beanSeed.setRiskType(Constants.MOMENTS_HLBEAN_CODE);
            R<Boolean> result = hxBeanService.updateHBean(beanSeed, SecurityConstants.INNER);
        }
        return toAjax(true);
    }

    @RequiresPermissions("biz:moments:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Moments domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }

    @RequiresPermissions("biz:moments:list")
    @PostMapping("/page")
    public AjaxResult page(@RequestBody Moments domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        page.addOrder(OrderItem.desc("moments_id"));
        Page<Moments> list = baseService.selectPageX(domain, page);
        return success(list);
    }

    @RequiresPermissions("biz:moments:list")
    @PostMapping("/page_old")
    public AjaxResult pageOld(@RequestBody Moments domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<Moments> page = Page.of(current, size);
        page.addOrder(OrderItem.desc("moments_id"));
        Page<Moments> list = baseService.page(page, Wrappers.query(domain));
        list.getRecords().forEach(el -> {
            List<String> urls = baseService.selectImageUrlByMomentsId(el.getMomentsId());
            el.setImages(urls);
        });

        return success(list);
    }
}